package com.zzzyh.springproject.controller;

import com.zzzyh.springproject.pojo.APIResponse;
import com.zzzyh.springproject.pojo.Category;
import com.zzzyh.springproject.service.CategoryService;
import com.zzzyh.springproject.utils.UpLoadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Controller
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    CategoryService categoryService;


    //    功能二：分类列表查询接口
    @RequestMapping("/getCategoryList")
    @ResponseBody
    public APIResponse<List<Category>> getList(){
        return APIResponse.success(categoryService.getList());
    }

    //    功能三：根据id查询分类信息接口
    @RequestMapping("/getCategory")
    @ResponseBody
    public APIResponse<Category> getCategory(Integer id){
        return APIResponse.success(categoryService.getCategory(id));
    }

    //    功能四：添加分类接口
    @RequestMapping("/addCategory")
    @ResponseBody
    public APIResponse<Void> addCategory(@RequestBody Category category)
    {
        categoryService.addCategory(category);
        return APIResponse.success(null);
    }

    //    功能五：修改分类信息接口
    @RequestMapping("/updateCategory")
    @ResponseBody
    public APIResponse<Void> updateCategory(@RequestBody Category category)
    {
       categoryService.updateCategory(category);
        return APIResponse.success(null);
    }

    //    功能六：删除分类接口
    @RequestMapping("/deleteCategory")
    @ResponseBody
    public APIResponse<Void> deleteCategory(Integer id)
    {
        categoryService.deleteCategory(id);
        return APIResponse.success(null);
    }

    //    功能七：上传图片接口
    @PostMapping("/uploadImage")
    @ResponseBody
    public APIResponse<String> uploadImage(@RequestParam("file") MultipartFile file) {
        String filePath = UpLoadUtil.uploadImage(file,"upload/image/category/myAdd");
        return APIResponse.success(filePath);
    }

}
